<?php

/**
 * Created by PhpStorm.
 * User: wu
 * Date: 2017/4/20
 * Time: 18:16
 */
class C_order_save
{
    private $resource;

    private $call_items;

    private $back_items;

    private $resource_id;

    private $payload;

    public function __construct()
    {
        $this->resource = 'C_Order';

        $this->call_items = array(
            "t" => time(),
            "k" => "",
            'uid' => '',
            'v' => API_VERSION,
            "userType" => ""
        );

        $this->back_items = array();
    }

    public function set_call_items($obj = array())
    {
        foreach ($this->call_items as $key => $val) {
            if (isset($obj[$key])) {
                $this->call_items[$key] = $obj[$key];
            }
        }
        $base_str = 'actionType=submit';
        if (isset($obj['uid'])) {
            $base_str .= '&uid=' . $obj['uid'];
        }
        if (isset($obj['channelId'])) {
            $base_str .= '&channelId=' . $obj['channelId'];
        }
        if (isset($obj['isPayType'])) {
            $base_str .= '&isPayType=' . $obj['isPayType'];
        }
        if (isset($obj['pay_type_item_id'])) {
            $base_str .= '&pay_type_item_id=' . $obj['pay_type_item_id'];
        }
        if (isset($obj['contact'])) {
            $base_str .= '&contact=' . $obj['contact'];
        }
        if (isset($obj['tel'])) {
            $base_str .= '&tel=' . $obj['tel'];
        }
        if (isset($obj['address'])) {
            $base_str .= '&address=' . $obj['address'];
        }
        if (isset($obj['remark'])) {
            $base_str .= '&remark=' . $obj['remark'];
        }
        if (isset($obj['deliveryTime'])) {
            $base_str .= '&deliveryTime=' . $obj['deliveryTime'];
        }
        $this->payload = base64_encode($base_str);
        $this->call_items['k'] = md5($obj['password'] . $this->resource_id . $this->call_items['t'] . $this->payload);
        return $this->_get_url();
    }

    public function set_back_items($data = array())
    {
        foreach ($this->back_items as $key => $val) {
            if (isset($data[$key])) {
                $this->back_items[$key] = $data[$key];
            }
        }
    }

    public function set_resource_id($id)
    {
        $this->resource_id = $id;
        return $this->_get_url();
    }

    public function get_call_items()
    {
        return 'payload=' . $this->payload;
    }

    public function get_back_items()
    {
        return $this->back_items;
    }

    public function get_resource_id()
    {
        return $this->resource_id;
    }

    private function _get_url()
    {
        $query_arr = array();
        foreach ($this->call_items as $key => $val) {
            $query_arr[] = $key . '=' . $val;
        }
        if ($this->resource_id) {
            return $this->resource . '/' . $this->resource_id . '/?' . implode('&', $query_arr);
        } else {
            return $this->resource . '/?' . implode('&', $query_arr);
        }
    }
}